/**
 * 
 */
package com.rhf.core.db.implementations.postgresql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.rhf.core.db.implementations.Base;
import com.rhf.core.db.interfaces.ParticiperCompetition;
import com.rhf.core.dto.ParticiperCompetitionVO;

/**
 * @author fsau
 * 
 */
public class ParticiperCompetitionImp extends Base implements ParticiperCompetition {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.rhf.core.db.interfaces.ParticiperCompetition#contructeur(java.sql
	 * .ResultSet)
	 */
	public ParticiperCompetitionVO contructeur(ResultSet rs) throws Exception {

		ParticiperCompetitionVO participerCompetitionVO = new ParticiperCompetitionVO();

		try {

			participerCompetitionVO.setCompetitionId(rs.getInt("competition_id"));
			participerCompetitionVO.setEquipeId(rs.getInt("equipe_id"));

		} catch (Exception e) {
			this.appLog.error(e.getMessage(), e);
			throw e;
		}

		return participerCompetitionVO;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seecom.rhf.core.db.interfaces.ParticiperCompetition#
	 * ajouterParticipationCompetition(com.rhf.core.dto.ParticiperCompetitionVO)
	 */
	public int ajouterParticipationCompetition(ParticiperCompetitionVO participerCompetitionVO) throws Exception {

		Connection conn = null;
		PreparedStatement stmt = null;
		int nbLigneAffecte = 0;

		// Requête SQL
		String query = "INSERT INTO participer_competition ";
		query += "(competition_id, equipe_id) ";
		query += "VALUES (?, ?)";

		try {

			// Connexion à la base de données
			conn = super.getConnection();
			stmt = conn.prepareStatement(query);

			// Définition des paramètres (?)
			int i = 1;
			stmt.setInt(i++, participerCompetitionVO.getCompetitionId());
			stmt.setInt(i++, participerCompetitionVO.getEquipeId());

			// Exécution de la Requête
			this.appLog.debug(stmt.toString());
			nbLigneAffecte = stmt.executeUpdate();

		} catch (Exception e) {
			this.appLog.error(e.getMessage(), e);
			throw e;
		} finally {
			super.closeResources(null, stmt, conn);
		}

		return nbLigneAffecte;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seecom.rhf.core.db.interfaces.ParticiperCompetition#
	 * supprimerParticipationCompetition
	 * (com.rhf.core.dto.ParticiperCompetitionVO)
	 */
	public int supprimerParticipationCompetition(ParticiperCompetitionVO participerCompetitionVO) throws Exception {

		Connection conn = null;
		PreparedStatement stmt = null;
		int nbLigneAffecte = 0;

		// Requête SQL
		String query = "DELETE FROM participer_competition ";
		query += "WHERE competition_id = ? ";
		query += "AND equipe_id = ? ";

		try {

			// Connexion à la base de données
			conn = super.getConnection();
			stmt = conn.prepareStatement(query);

			// Définition des paramètres (?)
			int i = 1;
			stmt.setInt(i++, participerCompetitionVO.getCompetitionId());
			stmt.setInt(i++, participerCompetitionVO.getEquipeId());

			// Exécution de la Requête
			this.appLog.debug(stmt.toString());
			nbLigneAffecte = stmt.executeUpdate();

		} catch (Exception e) {
			this.appLog.error(e.getMessage(), e);
			throw e;
		} finally {
			super.closeResources(null, stmt, conn);
		}

		return nbLigneAffecte;

	}

}
